<?php
include './include/common.inc.php';
include '../include/member.class.php';
include_once '../include/form.class.php';
include_once '../include/type.class.php';
$action = isset( $action ) ? $action : 'manage';
$forward = get_forward( 'user.php' );
$member_db = new member();
$type_db = new type();

if( $current_user === false )
{
	if( isset( $edufepm ) && $edufepm == 'iframe' )
	{
		$response->alert( '请您登陆后再访问' );
		$response->redirect( $base_dir . 'admin/login.php' );
		$response->execute();
	}
	else
	{
		showmessage( '请您登陆后再访问', $base_dir . 'admin/login.php' );
	}
}

switch( $action )
{
    case 'add':
        if( isset( $_POST['email'] ) )
		{
            $member = $member_db->get_member_by_email( $email );//获取数据库中是否有该用户
            if( isset($member['mid']) )
            {
                $response->text_alert( 'error','email','邮箱已存在,请重新输入' );
            }
			if( empty( $email ) )
			{
				$response->text_alert( 'error','email','请输入电子信箱' );
			}
            if( empty( $password ) )
            {
                $response->text_alert( 'error','password','用户密码不能为空' );
            }
            if( $password != $password2 )
            {
                $response->text_alert( 'error','password2','确认密码与设定密码不符,请重新确认' );
            }
            if( empty( $truename ) || strlen( $truename ) > 100 )
            {
                $response->text_alert( 'error','truename','真实姓名不能为空且长度不能大于100个字符' );
            }
            
			$response->real_execute();
            
            if(empty($types[1]))
            {
                $response->alert('请选择专业和班级' );
                $response->execute();
            }
			$params = array(
				'email'         =>  $email,
				'password'      =>  md5( $password ),
                'truename'      =>  $truename,
                'tel'           =>  $tel,
                'department'    =>  $department,
                'photo'         =>  $photo,
                'major'         =>  $types[0],
                'class'         =>  $types[1],
                'status'        =>  99,
                'addtime'       =>  time(),
                'lastlogin'     =>  0
			);
			$res = $member_db->add_member( $params );

			if( $res)
			{
				$response->alert( '添加成功' );
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '添加失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
			include template( 'admin', 'member_add' );
		}
        break;

    case 'upd':
        
        if( isset( $_POST['email'] ) )
		{
            $info = $member_db->get_member_by_id( $mid );//获取数据库中该用户的所有信息
            if( $info['email'] != $email )//如果修改了用户名，则检查数据库中用户是否存在
            {
                $check_res = $member_db->get_member_by_email( $email );
                if( isset($check_res['mid']) )
                {
                    $response->text_alert( 'error','email','email存在,请重新输入' );
                }
            }
			if( empty( $email ) )
			{
				$response->text_alert( 'error','email','email不能为空' );
			}
            if( !empty( $password ) )
            {
                if( $password != $password2 )
                {
                    $response->text_alert( 'error','password2','确认密码与设定密码不符,请重新确认' );
                }
                $upd_password = md5( $password );
            }
            else
            {
                $upd_password = $info['password'];
            }
            if( empty( $truename ))
            {
                $response->text_alert( 'error','truename','真实姓名不能为空' );
            }
			$response->real_execute();
            if(empty($types[1]))
            {
                $response->alert('请选择专业和班级' );
                $response->execute();
            }
			$params = array(
				'email'         =>  $email,
				'password'      =>  md5( $password ),
                'truename'      =>  $truename,
                'tel'           =>  $tel,
                'department'    =>  $department,
                'photo'         =>  $photo,
                'major'         =>  $types[0],
                'class'         =>  $types[1],
                'status'        =>  $status
			);
			$res = $member_db->edit_member( $params, $mid );

           

			if( !empty($res) )
			{
				$response->alert( '修改成功' );
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '修改失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
		}
		else
		{
			$info = $member_db->get_member_by_id( $mid );
			include template( 'admin', 'member_upd' );
		}
        break;


    case 'del_all':
        if( isset( $_POST['mid'] ) )
		{
			foreach ( $mid as $id )
			{
				$member_db->delete_member( $id );
			}
			$response->alert( '删除成功！' );
			$response->script( 'self.location=self.location' );
			$response->execute();
		}
		else
		{
			$response->alert( '请选择要删除的校友' );
			$response->execute();
		}
        break;

    

    case 'search':
        $url = 'member.php?page=1';
		$url .= isset( $email ) && !empty( $email ) ? '&email='.urlencode( $email ) : '';
        $url .= isset( $tel ) && !empty( $tel ) ? '&tel='.urlencode( $tel ) : '';
        $url .= isset( $truename ) && !empty( $truename ) ? '&truename='.urlencode( $truename ) : '';
        $url .= isset( $status ) && !empty( $status ) ? '&status='. $status : '';
        $url .= isset( $types[0] ) && !empty( $types[0] ) ? '&major='. $types[0] : '';
        $url .= isset( $types[1] ) && !empty( $types[1] ) ? '&class='. $types[1] : '';
		$response->redirect( $url );
		$response->execute();
        break;

    //管理留言
    case 'manage_ly':

        include_once APP_ROOT.'include/member.class.php';
        $member_ly = new member_ly();
        $member = new member();
        $type = new type();
        $page = isset($_GET['page']) ? max(1,intval($_GET['page'])) : 1;
        $pagesize = 10;
        $data = $member_ly->get_ly(array(), $page, $pagesize);

        $pages = $member_ly->pages;
        $typelist = $type->get_typs('ly');

        include template('admin', 'manage_ly');
        break;

    case 'audit_ly':

        include_once APP_ROOT.'include/member.class.php';
        $member_ly = new member_ly();
        if(!isset($ids) || !is_array($ids))
        {
            $response->alert('请选择要审核的留言');
            $response->execute();
        }

        foreach($ids as $id)
        {
            $member_ly->edit_ly(array('status'=>$status), $id);
        }
        $response->alert('审核成功');
        $response->script('self.location=self.location');
        $response->execute();
        break;

    case 'edit_ly':

        include_once APP_ROOT.'include/member.class.php';
        $member_ly = new member_ly();
        $type = new type();

        if(!isset($id))
        {
            $response->alert('请选择要修改的留言');
            $response->execute();
        }

        if(!empty($_POST))
        {
            $member_ly->edit_ly($data, $id);
            $response->alert('修改成功');
            $response->redirect(get_forward());
            $response->execute();
        }
        else
        {
            $typelist = $type->get_typs('ly');
            $info = $member_ly->get_ly_by_id($id);
            include template('admin', 'edit_ly');
        }
        break;
        
    case 'manage':
    
    default:
        $page = isset( $page ) ? intval( $page ) : 1;
		$page = max( $page, 1 );
        $pagesize = 10;
        $params = array();
        if(isset($_GET['email']))
        {
            $params['email'] = urldecode($_GET['email']);
        }
        if(isset($_GET['tel']))
        {
            $params['tel'] = urldecode($_GET['tel']);
        }
        if(isset($_GET['truename']))
        {
            $params['truename'] = urldecode($_GET['truename']);
        }
        if(isset($_GET['status']))
        {
            $params['status'] = $_GET['status'];
        }
        if(isset($_GET['major']))
        {
            $params['major'] = $_GET['major'];
        }
        if(isset($_GET['class']))
        {
            $params['class'] = $_GET['class'];
        }
        $major_types = $type_db->get_typs('major');
		$user_list = $member_db->get_members($params, $page, $pagesize);
        $user_pages = $member_db->pages;
        $forward = make_forward();
        $major_str = isset($class) ? $class : (isset($major) ? $major : '');
		include template( 'admin', 'member_manage' );
        break;
}

?>
